﻿using System.Runtime.InteropServices;

namespace Ast.Core.Pe.Definitions
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public struct SectionHeader
    {
        private const int ImageSizeofShortName = 8;

        public static readonly int StructSize = 0x28;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ImageSizeofShortName)]
        public string Name;

        public uint PhysicalAddressOrVirtualSize;

        public uint VirtualAddress;

        public uint SizeOfRawData;

        public uint PointerToRawData;

        public uint PointerToRelocations;

        public uint PointerToLinenumbers;

        public ushort NumberOfRelocations;

        public ushort NumberOfLinenumbers;

        public SectionCharacteristics Characteristics;

        public bool Contains(long virtualAddress)
        {
            return ((virtualAddress >= VirtualAddress) && (virtualAddress < VirtualAddress + SizeOfRawData));
        }

        public override string ToString()
        {
            return string.Format("{0}: VirtualAddress={1:X8}, Size={2:X8}", Name, VirtualAddress, SizeOfRawData);
        }
    }
}